package com.crm.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.crm.common.result.AjaxResult;
import com.crm.common.result.PageResult;
import com.crm.domain.DictData;
import com.crm.domain.DictType;
import com.crm.dto.DictDataPageQueryDto;
import com.crm.dto.DictTypePageQueryDto;
import com.crm.service.DictDataService;
import com.crm.service.DictTypeService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/system/dict")
public class DictController {

    private final DictDataService dictDataService;

    private final DictTypeService dictTypeService;

    //通过字典类型查询字典数据
    @GetMapping("/data/type/{dictType}")
    public AjaxResult getDictData(@PathVariable String dictType){
        List<DictData> dictDataList = dictDataService.list(Wrappers.<DictData>lambdaQuery().eq(DictData::getDictType, dictType));
        AjaxResult ajax = new AjaxResult();
        ajax.put("data", dictDataList);
        return ajax;
    }

    @GetMapping("/type/list")
    public PageResult page(DictTypePageQueryDto dto){
        PageResult pageResult = dictTypeService.pageDictType(dto);
        return pageResult;
    }

    @PostMapping("/type")
    public AjaxResult saveDictType(@RequestBody DictType dict){
        dictTypeService.save(dict);
        return AjaxResult.success();
    }


    @GetMapping("/type/{id}")
    public AjaxResult getById(@PathVariable Long id){
        return AjaxResult.success(dictTypeService.getById(id));
    }

    @PutMapping("/type")
    public AjaxResult updateById(@RequestBody DictType dict){
        dictTypeService.updateById(dict);
        return AjaxResult.success();
    }

    @DeleteMapping("/type/{ids}")
    public AjaxResult delete(@PathVariable List<Long> ids){
        dictTypeService.removeByIds(ids);
        return AjaxResult.success();
    }

    @GetMapping("/data/list")
    public AjaxResult pageDictData(DictDataPageQueryDto dto){
        return dictDataService.pageDictData(dto);
    }

    @PostMapping("/data")
    public AjaxResult saveDictData(@RequestBody DictData dictData){
        dictDataService.save(dictData);
        return AjaxResult.success();
    }

    @GetMapping("/data/{id}")
    public AjaxResult getDictDataById(@PathVariable Long id){
        return AjaxResult.success(dictDataService.getById(id));
    }

    @DeleteMapping("/data/{ids}")
    public AjaxResult deleteDictDataByIds(@PathVariable List<Long> ids){
        dictDataService.removeByIds(ids);
        return AjaxResult.success();
    }

    @PutMapping("/data")
    public AjaxResult updateDictDataById(@RequestBody DictData dictData){
        dictDataService.updateById(dictData);
        return AjaxResult.success();
    }

}
